package com.inossem.warehousestorage_inossem.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.inossem.utils.download.DownloadUtils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.utils.log.LogUtils;
import com.inossem.utils.scan.ScanUtils;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.yanzhenjie.nohttp.Headers;
import com.yanzhenjie.nohttp.download.DownloadListener;
import com.yanzhenjie.nohttp.download.DownloadRequest;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class TestActivity extends BaseActivity {

    private TextView title;

    private Button button1, button2, button3;

    private TextView textView;

    private DownloadRequest mDownloadRequest;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_test);
        initView(TestActivity.this);
        initData(TestActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        button1 = (Button) activity.findViewById(R.id.button1);
        button2 = (Button) activity.findViewById(R.id.button2);
        button3 = (Button) activity.findViewById(R.id.button3);
        textView = (TextView) activity.findViewById(R.id.textView);
    }

    private void initData(Activity activity) {
        title.setText("测试");
        button1.setText("下载测试,注释");
        button1.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
//                // 开始下载了，但是任务没有完成，代表正在下载，那么暂停下载。
//                if (mDownloadRequest != null && mDownloadRequest.isStarted() && !mDownloadRequest.isFinished()) {
//                    // 暂停下载。
//                    mDownloadRequest.cancel();
//                } else if (mDownloadRequest == null || mDownloadRequest.isFinished()) {// 没有开始或者下载完成了，就重新下载。
//                    mDownloadRequest = DownloadUtils.getApkDownloadRequest(activity, "http://192.168.0.17:8080/wms/portable/download/youku.apk", "aaa.apk");
//                    // what 区分下载。
//                    // downloadRequest 下载请求对象。
//                    // downloadListener 下载监听。
//                    DownloadUtils.getInstance().download(TestActivity.this, 8957, mDownloadRequest, downloadListener);
//                }
            }
        });
        button2.setText("测试2");
        button2.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                List<String> list = new ArrayList<>();
                list.add("1");
                list.add("2");
                list.add("3");
                list.add("4");
                list.add("5");
                LogUtils.i("Before swap: " + list);

                // swap the elements
                Collections.swap(list, 0, 4);

                LogUtils.i("After swap: " + list);
                Collections.sort(list);
                LogUtils.i("After sort: " + list);

            }
        });
        button3.setText("扫描");
        button3.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                ScanUtils.openScanActivity(TestActivity.this);
            }
        });

        setmScanCodeListener(new ScanUtils.ScanCodeListener() {
            @Override
            public void onResult(String scanResult) {
                textView.setText("修改后扫描内容：" + scanResult);
            }
        });
    }

    /**
     * 下载监听
     */
    private DownloadListener downloadListener = new DownloadListener() {

        @Override
        public void onStart(int what, boolean isResume, long beforeLength, Headers headers, long allCount) {
            LogUtils.i("onStart:what【" + what + "】---isResume【" + isResume + "】---beforeLength【" + beforeLength + "】---allCount【" + allCount + "】");
        }

        @Override
        public void onDownloadError(int what, Exception exception) {
            LogUtils.i("onDownloadError:what【" + what + "】---exception【" + exception.toString() + "】---messageContent【" + DownloadUtils.getErroMessage(exception) + "】");
        }

        @Override
        public void onProgress(int what, int progress, long fileCount, long speed) {
            LogUtils.i("onProgress:what【" + what + "】---progress【" + progress + "】---fileCount【" + fileCount + "】---speed【" + speed + "】");
        }

        @Override
        public void onFinish(int what, String filePath) {
            LogUtils.i("onFinish:what【" + what + "】---filePath【" + filePath + "】");
        }

        @Override
        public void onCancel(int what) {
            LogUtils.i("onCancel:what【" + what + "】");
        }

    };

}
